package org.zjh.controller.timer;


import org.zjh.controller.GameController;
       import org.zjh.model.Player;
import org.zjh.model.RealPlayer;
import org.zjh.utils.MySQLUtil;
import org.zjh.utils.PokerUtil;
import org.zjh.view.game.GameOverFrame;
import org.zjh.view.game.GamePanel;
import org.zjh.view.ui.GameUi;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @description: 出牌定时器
 * @author 郑佳豪
 * @date 2024/10/16 14:27
 * @version 1.0
 */
public class DiscardPokersTimer {
    private GamePanel gamePanel ;
    private GameController gameController = GameController.getInstance();
    private int countdown = 20; // 叫分时间
    private Player firstPlayer;
    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Player curDiscardPokerPlayer = gameController.getCurDiscardPokerPlayer();
            JLabel countdownLabel = gamePanel.getCountdownJLabel();
            countdownLabel.setText(--countdown + "秒");
            //判断玩家在时间内出牌是否完成
            if (curDiscardPokerPlayer.isDiscardPokerOver()) {
                //出牌结束
                gameController.setCurDiscardPokerPlayer(gameController.getCurDiscardPokerPlayer());
                if (curDiscardPokerPlayer instanceof RealPlayer) {
                    gamePanel.setDiscardPokerVisibleFalse();
                }
                countTimer.stop();
                switchNextPlayer();
            }
            //超时默认不出
            if (countdown <= 0) {
                //只有 实际玩家会超时 默认不出  如果实际玩家是地主玩家 并且 当前牌桌上的牌为空 或者 当前牌桌上的牌是 上一轮 实际玩家出的那么 不能选择不出
                if (firstPlayer == curDiscardPokerPlayer
                        && (gameController.getNowDeskPokerList().isEmpty() || gameController.getNowDeskPokerList().get(0).getOwnerId().equals("REAL"))){
                    //超时了 那么我们就强制出牌
                    PokerUtil.randomDiscardPoker(curDiscardPokerPlayer);
                    PokerUtil.finishDiscardPoker(curDiscardPokerPlayer,gameController.getNowDeskPokerList());
                }else{ //如果不是不得不出 超时了 默认不要
                    //结束出牌
                    curDiscardPokerPlayer.setDiscardPokerOver(true);
                    //设置不要
                    gamePanel.getRealNoDiscardJLabel().setText(("不要"));
                    //设置 玩家出牌选项不可见
                    gamePanel.setDiscardPokerVisibleFalse();
                }
                //切换下一个玩家
                switchNextPlayer();
            }
        }
    };

    private Timer countTimer = new Timer(1000, actionListener);

    /**
     * 开始出牌
     * @param curDiscardPokerPlayer 当前出牌玩家
     */
    public void startDiscardPoker(Player curDiscardPokerPlayer) {


        gameController.setCurDiscardPokerPlayer(curDiscardPokerPlayer);
        //出牌倒计时重置
        countdown = 20;
        //获取游戏面板中倒计时标签
        JLabel countdownLabel = gamePanel.getCountdownJLabel();
        //根据出牌玩家 设置倒计时标签位置
        gamePanel.setCountdownLabel(countdownLabel, gameController, curDiscardPokerPlayer);
        //停止上一次倒计时
        if (countTimer != null) {
            countTimer.stop();
        }
        //倒计时标签赋值
        countdownLabel.setText(countdown + "秒");
        //如果是实际玩家 那么显示出牌选项
        if (curDiscardPokerPlayer instanceof RealPlayer) {

            //判断实际玩家是否实在托管阶段 如果是那么由机器人代打
            if (((RealPlayer) curDiscardPokerPlayer).isHosted()){
                //设置手牌不可点击
                PokerUtil.setCantBeClick(curDiscardPokerPlayer.getPokerList());
                //自动出牌
                PokerUtil.autoDiscardPoker(curDiscardPokerPlayer,gameController.getNowDeskPokerList());
            }else{
                //设置手牌可以点击
                PokerUtil.setCanBeClick(curDiscardPokerPlayer.getPokerList());
                if (firstPlayer == curDiscardPokerPlayer
                        && (gameController.getNowDeskPokerList().isEmpty() || gameController.getNowDeskPokerList().get(0).getOwnerId().equals("REAL"))) {
                    //如果实际玩家是地主玩家 并且 当前牌桌上的牌为空 或者 当前牌桌上的牌是 上一轮 实际玩家出的那么 不能选择不出
                    gamePanel.getPromptJLabel().setVisible(true);
                    gamePanel.getDiscardPokersJLabel().setVisible(true);
                } else {
                    gamePanel.setDiscardPokerVisibleTrue();
                }
            }
        } else {
            curDiscardPokerPlayer.discardPoker();
        }
        //重绘游戏面板
        gamePanel.repaint();
        countTimer.start();


    }

    /**
     * 切换到下一个玩家
     */
    public void switchNextPlayer() {


        //停止上一次倒计时
        countTimer.stop();
        Player curDiscardPokerPlayer = gameController.getCurDiscardPokerPlayer();
        //重置当前玩家的 出牌是否完成的标志
        curDiscardPokerPlayer.setDiscardPokerOver(false);
        //首先判断 当前玩家手中的牌是否出完了 如果是 结束
        if (curDiscardPokerPlayer.getPokerList().isEmpty()) {
            //创建结算窗口
            GameUi.gameOverFrame = new GameOverFrame(curDiscardPokerPlayer);
            //更新数据库
            //判断当前实际玩家是不是游客 不是的话更新数据库
            if (gameController.getPlayer().getAccount() != null){
                MySQLUtil.updatePlayerScore(gameController.getPlayer());
            }

            return;
        }
        //如果没出完 换一下个
        //当前出牌玩家等于 当前出牌玩家的下一个出牌玩家
        gameController.setCurDiscardPokerPlayer(gameController.getCurDiscardPokerPlayer().getNextPlayer());
        startDiscardPoker(gameController.getCurDiscardPokerPlayer());

    }


    /**
     * 开始
     * @param player 第一个出牌的玩家 （地主玩家）
     */
    public void start(Player player) {
        gamePanel = GameUi.gameFrame.getGamePanel();
        PokerUtil.setCanBeClick(GameController.getInstance().getPlayer().getPokerList()); //将实际玩家手牌设置可点击
        firstPlayer = player;
        startDiscardPoker(player);
    }

    public Timer getCountTimer() {
        return countTimer;
    }
}
